package com.gykfbxxh.imovie.view.lunch;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

import com.gykfbxxh.imovie.R;
import com.gykfbxxh.imovie.adapter.WelcomePagerAdapter;
import com.gykfbxxh.imovie.base.BaseActivity;
import com.gykfbxxh.imovie.config.ConfigKey;
import com.gykfbxxh.imovie.manager.AppConfigManager;

import java.util.ArrayList;

public class Welcome extends BaseActivity implements OnClickListener, OnPageChangeListener {
    private ViewPager viewPager;// 图片
    //	private LinearLayout linearLayout;// 小圆点布局
    private WelcomePagerAdapter welcomePagerAdapter;
    private ArrayList<View> views;
    //	private ArrayList<ImageView> icons;// 小圆点
    public static final String INTENT_AD_APP_INFO = "intent_ad_app_info";

    private String app_url, image_url;
    private int currentState = 0;
    private int slideLeft = 1;
    private int slideRight = 2;
    private float dowmX;
    private boolean isShowAd = false;//是否显示广告
    private boolean isHasAd = false;//是否有广告
    private ImageView imageView1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcome);
        isSlideBack = false;
        initView();
        initData();
    }

    private void initData() {
        welcomePagerAdapter = new WelcomePagerAdapter(views);
        viewPager.setOnPageChangeListener(this);
        viewPager.setAdapter(welcomePagerAdapter);
        SharedPreferences.Editor editor = AppConfigManager.getInstance().getEditor();
        editor.putBoolean(ConfigKey.isStartWelcome,true);
        editor.commit();
    }

    /**
     * 初始化控件
     */
    private void initView() {
        views = new ArrayList<View>();
//		icons = new ArrayList<ImageView>();

        viewPager = (ViewPager) findViewById(R.id.viewPager);
//		linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
        LayoutInflater inflater = LayoutInflater.from(this);

        View view1 = inflater.inflate(R.layout.welcome_first, null);
        View view2 = inflater.inflate(R.layout.welcome_two, null);
		View view3 = inflater.inflate(R.layout.welcome_three, null);

        imageView1 = (ImageView)view3.findViewById(R.id.imageView1);
        imageView1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(Welcome.this, LunchActivity.class));
                overridePendingTransition(R.anim.base_slide_right_in, R.anim.base_slide_remain);
                onBackPressed();
            }
        });
//		ImageView imageView1 = (ImageView) view1.findViewById(R.id.imageView1);
//		AppViewUtils.chengeViewFullScreenL(imageView1);
//
//		imageView1 = (ImageView) view2.findViewById(R.id.imageView1);
//		AppViewUtils.chengeViewFullScreenL(imageView1);
//
//		imageView1 = (ImageView) view3.findViewById(R.id.imageView1);
//		AppViewUtils.chengeViewFullScreenL(imageView1);

        views.add(view1);
        views.add(view2);
		views.add(view3);

//		ImageView imageView = new ImageView(this);
//		imageView.setImageResource(R.drawable.intro_2_indicator);
//		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//		lp.setMargins(0, 0, 20, 0);
//		imageView.setLayoutParams(lp);
//		icons.add(imageView);
//		linearLayout.addView(imageView);
//
//		imageView = new ImageView(this);
//		imageView.setImageResource(R.drawable.intro_1_indicator);
//		lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//		lp.setMargins(0, 0, 20, 0);
//		imageView.setLayoutParams(lp);
//		icons.add(imageView);
//		linearLayout.addView(imageView);
//
//		imageView = new ImageView(this);
//		imageView.setImageResource(R.drawable.intro_1_indicator);
//		imageView.setLayoutParams(lp);
//		icons.add(imageView);
//		linearLayout.addView(imageView);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub'
        if (isHasAd && views != null && viewPager.getCurrentItem() == views.size() - 1) {

            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    currentState = 0;
                    dowmX = event.getX();
                    break;
                case MotionEvent.ACTION_MOVE:
                    float slide = event.getX() - dowmX;
                    if (slide < -30) {
                        currentState = slideLeft;
                    } else if (slide > 30) {
                        currentState = slideRight;
                    } else {
                        currentState = 0;
                    }
                    break;

                default:
                    if (currentState == slideLeft) {
//                        onSlideLeft();
                    }
                    break;
            }
        } else if (!isHasAd && views != null && viewPager.getCurrentItem() == views.size() - 1) {

            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    currentState = 0;
                    dowmX = event.getX();
                    break;
                case MotionEvent.ACTION_MOVE:
                    float slide = event.getX() - dowmX;
                    if (slide < -30) {
                        currentState = slideLeft;
                    } else if (slide > 30) {
                        currentState = slideRight;
                    } else {
                        currentState = 0;
                    }
                    break;

                default:
                    if (currentState == slideLeft) {
                        startActivity(new Intent(this, LunchActivity.class));
                        overridePendingTransition(R.anim.base_slide_right_in, R.anim.base_slide_remain);
                        onBackPressed();
                    }
                    break;
            }
        }
        return super.dispatchTouchEvent(event);
    }

    /**
     * 左滑
     */
//    private void onSlideLeft() {
//        Intent intent = new Intent(this, DownAppService.class);
//        intent.putExtra("name", downLoadApp.getApp_name());
//        intent.putExtra("url", app_url);
//        startService(intent);//启动服务
//        setResult(RESULT_OK, getIntent());
//        onBackPressed();
//    }
//

    @Override
    public void onClick(View v) {
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return true;
    }


    @Override
    public void onPageScrollStateChanged(int arg0) {
    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
    }

    @Override
    public void onPageSelected(int arg0) {
//		for (int i = 0; i < icons.size(); i++) {
//			icons.get(i).setImageResource(R.drawable.intro_1_indicator);
//		}
//		icons.get(arg0).setImageResource(R.drawable.intro_2_indicator);
    }
}
